<template id="login-form">
  <div class="login-form">
    <div class="login-wrapper">
      <div class="login-left">
        <div class="title">网络安全综合服务平台</div>
      </div>
      <transition name="fade-transform" mode="out-in">
        <form class="login-right" @submit.prevent v-if="type === 'login'">
          <div class="h2">登录</div>
          <div class="form-group">
            <input
              type="text"
              id="username"
              placeholder="用户名"
              v-model="data.account"
            />
          </div>
          <div class="form-group">
            <input
              type="password"
              id="Password"
              placeholder="密码"
              v-model="data.password"
            />
          </div>
          <div class="goRegister">
            <a style="cursor: pointer" @click="goRegister">去注册！</a>
          </div>
          <div class="button-area">
            <el-button
              style="width: 100%"
              @click="loginAction"
              type="primary"
              size="large"
            >
              提交
            </el-button>
          </div>
        </form>

        <form class="login-right" @submit.prevent v-else>
          <div class="h2">注册</div>
          <div class="form-group">
            <input
              type="text"
              id="username"
              placeholder="用户名"
              v-model="data.account"
            />
          </div>
          <div class="form-group">
            <input
              type="password"
              id="Password"
              placeholder="密码"
              v-model="data.password"
            />
          </div>
          <div class="goRegister">
            <a style="cursor: pointer" @click="goLogin">去登录！</a>
          </div>
          <div class="button-area">
            <el-button
              style="width: 100%"
              @click="registerAction"
              type="primary"
              size="large"
            >
              注册
            </el-button>
          </div>
        </form>
      </transition>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { register } from '@/api/user.js';
import { ElMessage } from 'element-plus';
import store from '@/store';

const router = useRouter();
const data = ref({
  account: '',
  password: '',
});
const type = ref('login');
const goRegister = () => {
  type.value = 'register';
  data.value.account = '';
  data.value.password = '';
};
const goLogin = () => {
  type.value = 'login';
  data.value.account = '';
  data.value.password = '';
};
const loginAction = () => {
  store.dispatch('user/login', {
    account: data.value.account,
    password: data.value.password,
  });
};
const registerAction = () => {
  register({
    account: data.value.account,
    password: data.value.password,
  }).then((res) => {
    if (res.code === 200) {
      ElMessage.success(res.data);
    }
  });
};
onMounted(() => {
  let init = 1000;
  setTimeout(function () {
    document.querySelector('.login-wrapper').classList.toggle('open');
    init = 300;
  }, init);
});
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
* {
  box-sizing: border-box;
}
.goRegister {
  width: 100%;
  padding: 10px 0;
  margin-bottom: -24px;
  text-align: right;
}
.login-form {
  width: 100vw;
  height: 100vh;
  background: linear-gradient(45deg, #ffffff00, #cccccca9);

  display: grid;
  place-items: center;
}

#sandbox {
  font-family: 'Lato', sans-serif;
  display: -webkit-box;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  flex-direction: row-reverse;
  -webkit-box-pack: center;
  -webkit-justify-content: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-box-align: center;
  -webkit-align-items: center;
  -ms-flex-align: center;
  align-items: center;
  background: linear-gradient(243.87deg, #22bf64 30.6%, #371933 130.6%);
  overflow: hidden;
}

input {
  font-family: inherit;
  -webkit-appearance: none;
  -moz-appearance: none;
  border: 0;
  font-size: 16px;
  color: #000;
  border-radius: 0;
  border-botton: 0;
}

input[type='text'],
input[type='password'] {
  width: 100%;
  height: 40px;
  border-bottom: 1px solid #aaaaaa;
}

button,
input:focus {
  outline: 0;
}

::-webkit-input-placeholder {
  font-size: 16px;
  font-weight: 300;
  letter-spacing: -0.00933333em;
}

.form-group {
  position: relative;
  padding-top: 15px;
  margin-top: 10px;
}

label {
  position: absolute;
  top: 0;
  opacity: 1;
  -webkit-transform: translateY(5px);
  transform: translateY(5px);
  color: #aaa;
  font-weight: 300;
  font-size: 13px;
  letter-spacing: -0.00933333em;
  transition: all 0.2s ease-out;
}

input:placeholder-shown + label {
  opacity: 0;
  -webkit-transform: translateY(15px);
  transform: translateY(15px);
}

.h1 {
  color: #fff;
  opacity: 0.8;
  font-size: 20px;
  font-weight: 400;
  text-transform: uppercase;
  letter-spacing: 0.2405em;
  transition: all 770ms cubic-bezier(0.51, 0.04, 0.12, 0.99);
  text-align: center;
  cursor: pointer;
  position: absolute;
  transform: translateY(-10px);
}

.open .h1 {
  -webkit-transform: translateX(200px) translateZ(0) translateY(-10px);
  transform: translateX(200px) translateZ(0) translateY(-10px);
}

.h2 {
  color: #000;
  font-size: 20px;
  letter-spacing: -0.00933333em;
  font-weight: 600;
  padding-bottom: 15px;
}

.login-wrapper {
  width: 800px;
  height: 320px;
  background-color: #fff;
  box-shadow: 0px 2px 50px rgba(0, 0, 0, 0.2);
  border-radius: 15px;
  overflow: hidden;
  position: relative;
}

.login-left {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  transition: all 770ms cubic-bezier(0.51, 0.04, 0.12, 0.99);
  overflow: hidden;
}

.login-left .title {
  display: block;
  transition: all 770ms cubic-bezier(0.51, 0.04, 0.12, 0.99);
  object-position: left;
  font-size: 32px;
  font-weight: 500;
}

.open .login-left .title {
  -webkit-transform: translateX(210px) translateZ(0);
  transform: translateX(210px) translateZ(0);
}

.open .login-left {
  -webkit-transform: translateX(-400px) translateZ(0);
  transform: translateX(-400px) translateZ(0);
}

.login-right {
  padding: 40px;
  position: absolute;
  top: 0;
  right: 0;
  width: 400px;
  -webkit-transform: translateX(400px) translateZ(0);
  transform: translateX(400px) translateZ(0);
  transition: all 770ms cubic-bezier(0.51, 0.04, 0.12, 0.99);
}

.open .login-right {
  -webkit-transform: translateX(0px) translateZ(0);
  transform: translateX(0px) translateZ(0);
}

.checkbox-container {
  display: flex;
  margin-top: 35px;
}

.text-checkbox {
  color: #aaa;
  font-size: 16px;
  letter-spacing: -0.00933333em;
  font-weight: 300;
  margin-left: 15px;
}

input[type='checkbox'] {
  cursor: pointer;
  margin: 0;
  height: 22px;
  position: relative;
  width: 22px;
  -webkit-appearance: none;
  transition: all 180ms linear;
}

input[type='checkbox']:before {
  border: 1px solid #aaa;
  background-color: #fff;
  content: '';
  width: 20px;
  height: 20px;
  display: block;
  border-radius: 2px;
  transition: all 180ms linear;
}

input[type='checkbox']:checked:before {
  background: linear-gradient(198.08deg, #22bf64 45.34%, #e281e7 224.21%);
  border: 1px solid #22bf64;
}

input[type='checkbox']:after {
  content: '';
  border: 2px solid #fff;
  border-right: 0;
  border-top: 0;
  display: block;
  height: 4px;
  left: 4px;
  opacity: 0;
  position: absolute;
  top: 6px;
  -webkit-transform: rotate(-45deg);
  transform: rotate(-45deg);
  width: 12px;
  transition: all 180ms linear;
}

input[type='checkbox']:checked:after {
  opacity: 1;
}

.button-area {
  display: flex;
  justify-content: space-between;
  margin-top: 30px;
  width: 100%;
}

.btn {
  font-family: inherit;
  -webkit-appearance: none;
  -moz-appearance: none;
  background-color: transparent;
  border: none;
  border-radius: 2px;
  height: 40px;
  display: flex;
  padding: 0 35px;
  cursor: pointer;
  font-size: 16px;
  text-transform: uppercase;
  letter-spacing: -0.00933333em;
}

.btn-primary {
  color: #fff;
  background: linear-gradient(198.08deg, #22bf64 45.34%, #e281e7 224.21%);
  box-shadow: 0px 2px 10px rgba(0, 0, 0, 0.1);
  text-align: right;
}

.btn-primary:hover,
.btn-primary:focus {
  color: #fff;
  background: linear-gradient(198.08deg, #1b984f 45.34%, #e281e7 224.21%);
}

.btn-secondary {
  color: #22bf64;
}
.btn-secondary:focus,
.btn-secondary:hover {
  color: #1b984f;
}
</style>
